package org.nidy.programexercise.ThreadExercise;

public class Consumer extends Thread{

    @Override
    public void run() {
        while (true){
            synchronized (Intermediary.lock){
                if(Intermediary.count==0){
                    break;
                }else{
                    if(Intermediary.flag==0){
                        try {
                            //等待生产者生产
                            Intermediary.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else{

                        System.out.println("消费者正在消费第"+Intermediary.count+"个产品");
                        Intermediary.count--;
                        Intermediary.flag=0;
                        //通知生产者生产,唤醒线程
                        Intermediary.lock.notify();
                    }
                }
            }
        }
    }
}
